package com.yousu.dashijixm.service.impl;

import com.yousu.dashijixm.mapper.UserMapper;
import com.yousu.dashijixm.pojo.User;
import com.yousu.dashijixm.service.UserService;
import com.yousu.dashijixm.utils.Md5Util;
import com.yousu.dashijixm.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * @Author: YouSu
 * @Version: 1.0
 * @CreateTime: 2023-11-18  12:55
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public User findByUserName(String username) {
        //调用数据层查询用户信息
        User u = userMapper.findByUserName(username);
        return u;
    }

    @Override
    public void register(String username, String password) {
        //加密密码
        String md5String = Md5Util.getMD5String(password);
        //调用数据层添加信息到数据库
        userMapper.add(username, md5String);
    }

    @Override
    public void update(User user) {
        user.setUpdateTime(LocalDateTime.now());
        userMapper.update(user);

    }

    @Override
    public void updateAvatar(String avatarUrl) {
        //根据在拦截器中存储在ThreadLocal里的令牌获取id
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");

        userMapper.updateAvatar(avatarUrl,id);
    }

    @Override
    public void updatePwd(String new_pwd) {
        //根据在拦截器中存储在ThreadLocal里的令牌获取id
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
//        System.out.println(id);
        //传入加密的新密码
        userMapper.updatePwd(Md5Util.getMD5String(new_pwd),id);
    }
}
